package cn.ffcs.uec.staff.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.staff.component.bean.UecWorkListboxBean;
import cn.ffcs.uec.staff.constant.UecStaffConstants;
import cn.ffcs.uec.staff.manager.UecWorkManager;
import cn.ffcs.uec.staff.model.UecStaffInfo;
import cn.ffcs.uec.staff.model.UecWork;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

@Controller
@Scope("prototype")
public class UecWorkListboxExt extends Div implements IdSpace {

    private static final long serialVersionUID = -5811398149406451671L;

    /**
     * 人员基本信息功能 查询出的实体对象
     */
    private UecStaffInfo uecStaffInfo;

    /**
     * 对应页面上的bean
     */
    private UecWorkListboxBean bean = new UecWorkListboxBean();

    /**
     * 人员语言能力实体
     */
    private UecWork uecWork;

    /**
     * 人员语言能力查询实体
     */
    // private UecWork qryUecWork;
    private UecWorkManager uecWorkManager = (UecWorkManager) ApplicationContextUtil.getBean("uecWorkManager");

    @Getter
    @Setter
    private IPortletInfoProvider portletInfoProvider;

    public UecWorkListboxExt() {
        Executions.createComponents(UecStaffConstants.UEC_WORK_EXT_LISTBOX_ZUL, this, null);
        Components.wireVariables(this, bean);
        Components.addForwards(this, this, '$');
        this.addForward(UecStaffConstants.ON_UEC_WORK_QUERY, this, UecStaffConstants.ON_UEC_WORK_QUERY_RESPONSE);
        this.addForward(UecStaffConstants.ON_CLEAN_UEC_WORK, this, UecStaffConstants.ON_CLEAN_UEC_WORK_RESPONSE);
    }

    /**
     * 初始化
     */
    public void onCreate() {
        this.setButtonValid(false, false, false, false);
    }

    /**
     * 选择查询记录
     * 
     * @throws Exception
     */
    public void onSelectUecWorkList() throws Exception {
        if (this.bean.getUecWorkListbox().getSelectedCount() > 0) {
            uecWork = (UecWork) bean.getUecWorkListbox().getSelectedItem().getValue();
        }
    }

    /**
     * 人员基本信息功能，抛出事件， 事件监听
     * 
     * @param event
     */
    public void onUecWorkQueryResponse(ForwardEvent event) {
        uecStaffInfo = (UecStaffInfo) event.getOrigin().getData();
        if (null != uecStaffInfo) {
            uecWork = new UecWork();
            if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkPsndoc())
                    && !StrUtil.isNullOrEmpty(uecStaffInfo.getStaffId())) {
                uecWork.setPkPsndoc(uecStaffInfo.getPkPsndoc());
                setButtonValid(true, false, false, false);
            } else {
                uecWork.setStaffId(uecStaffInfo.getStaffId());
                this.setButtonValid(true, true, true, true);
            }
            this.onQueryUecWork();
        }
    }

    public void onCleanUecWorkResponse(ForwardEvent event) {
        clearVariable();
        uecStaffInfo = null;
        ListboxUtils.clearListbox(this.bean.getUecWorkListbox());
        this.bean.getUecWorkListboxPaging().setTotalSize(1);
    }

    /**
     * 设置按钮
     * 
     * @param canAdd
     * @param candel
     */
    private void setButtonValid(boolean canView, boolean canAdd, boolean canEdit, boolean candel) {
        this.bean.getViewButton().setDisabled(!canView);
        this.bean.getAddButton().setDisabled(!canAdd);
        this.bean.getEditButton().setDisabled(!canEdit);
        this.bean.getDelButton().setDisabled(!candel);
    }

    /**
     * 查询按钮
     */
    public void onQueryUecWork() {
        this.bean.getUecWorkListboxPaging().setActivePage(0);
        this.onQueryUecWorkList();
    }

    /**
     * 重置按钮
     */
    public void onResetUecWork() {
        ListboxUtils.clearListbox(this.bean.getUecWorkListbox());
        this.bean.getUecWorkListboxPaging().setTotalSize(1);
    }

    /**
     * 查看按钮
     * 
     * @throws Exception
     * @throws InterruptedException
     */
    public void onViewUecWork() throws Exception, InterruptedException {
        if (uecWork != null && null != uecWork.getId()) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("opType", "view");
            map.put("uecWork", uecWork);
            Window window = (Window) Executions.createComponents(UecStaffConstants.UEC_WORK_EDIT_ZUL, this, map);
            window.doModal();
            window.addEventListener("onOK", new EventListener() {

                @Override
                public void onEvent(Event event) throws Exception {
                    if (event.getData() != null) {
                        //
                    }
                }
            });
        } else {
            ZkUtil.showError("请选中要查看的记录", "提示信息");
        }
    }

    /**
     * 新增按钮
     * 
     * @throws Exception @throws
     */
    public void onAddUecWork() throws Exception {
        if (uecStaffInfo != null) {
            if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkPsndoc())
                    && !StrUtil.isNullOrEmpty(uecStaffInfo.getStaffId())) {
                ZkUtil.showError("集团下发数据，禁止操作！", "温馨提示");
            } else {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("opType", "add");
                map.put("uecStaffInfo", uecStaffInfo);
                Window window = (Window) Executions.createComponents(UecStaffConstants.UEC_WORK_EDIT_ZUL, this, map);
                window.doModal();
                window.addEventListener("onOK", new EventListener() {

                    @Override
                    public void onEvent(Event event) throws Exception {
                        if (event.getData() != null) {
                            // 新增或修改完成后，列表从数据库更新
                            uecWork = (UecWork) event.getData();
                            onQueryUecWorkList();
                        }
                    }
                });
            }
        }
    }

    /**
     * 编辑按钮
     * 
     * @throws Exception
     * @throws InterruptedException
     */
    public void onEditUecWork() throws Exception, InterruptedException {
        if (uecWork != null && null != uecWork.getId()) {
            if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkPsndoc())
                    && !StrUtil.isNullOrEmpty(uecStaffInfo.getStaffId())) {
                ZkUtil.showError("集团下发数据，禁止操作！", "温馨提示");
            } else {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("opType", "edit");
                map.put("uecWork", uecWork);
                Window window = (Window) Executions.createComponents(UecStaffConstants.UEC_WORK_EDIT_ZUL, this, map);
                window.doModal();
                window.addEventListener("onOK", new EventListener() {

                    @Override
                    public void onEvent(Event event) throws Exception {
                        if (event.getData() != null) {
                            // 新增或修改完成后，列表从数据库更新
                            uecWork = (UecWork) event.getData();
                            onQueryUecWorkList();
                        }
                    }
                });
            }
        } else {
            ZkUtil.showError("请选择要编辑的记录", "提示信息");
            return;
        }
    }

    /**
     * 删除按钮
     */
    public void onDelUecWork() {
        if (uecWork != null && this.uecWork.getId() != null) {
            if (!StrUtil.isNullOrEmpty(uecStaffInfo.getPkPsndoc())
                    && !StrUtil.isNullOrEmpty(uecStaffInfo.getStaffId())) {
                ZkUtil.showError("集团下发数据，禁止操作！", "温馨提示");
            } else {
                ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        Integer result = (Integer) event.getData();
                        if (result == Messagebox.OK) {
                            uecWork.removeOnly();
                            PubUtil.reDisplayListbox(bean.getUecWorkListbox(), uecWork, "del");
                            uecWork = null;
                        }
                    }
                });
            }
        } else {
            ZkUtil.showError("请选择你要删除的记录", "提示信息");
            return;
        }
    }

    /**
     * 翻页
     */
    public void onUecWorkListboxPaging() {
        this.onQueryUecWorkList();
    }

    /**
     * 清除变量
     */
    public void clearVariable() {
        uecWork = null;
    }

    /**
     * 查询
     */
    public void onQueryUecWorkList() {
        if (null != uecWork) {
            PageInfo pageInfo = uecWorkManager.queryPageInfoByUecWork(uecWork,
                    this.bean.getUecWorkListboxPaging().getActivePage() + 1,
                    this.bean.getUecWorkListboxPaging().getPageSize());
            ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
            this.bean.getUecWorkListbox().setModel(list);
            this.bean.getUecWorkListboxPaging().setTotalSize(pageInfo.getTotalCount());
        }
    }
}
